0% completed
Default parameters allow functions to have pre-defined values if no arguments are provided, or if undefined is passed. They simplify function calls and enhance the functionality of functions without the need for additional checks inside the function body.
Why We Need Default Parameters?
In earlier versions of JavaScript, handling missing arguments required additional code inside functions to assign default values. This could lead to verbose and less readable code, especially in functions with multiple parameters. Default parameters address this issue by allowing developers to set default values right in the function signature, making the function easier to use and understand.
Example: Function Without Default Parameters
Initially, handling missing arguments might look something like this:
- This example uses a logical OR (
||) to provide a default value ('Guest') ifnameis not provided or isundefined. - Calling
greet()without an argument defaults to greeting a 'Guest'.
Example: Function With Default Parameters
With ES6 default parameters, the same functionality can be achieved more cleanly:
- The function
greetnow directly assigns a default value ('Guest') tonamein the function signature. - This makes the code cleaner and the default value assignment more explicit.
Default Parameters with Multiple Arguments
Default parameters are especially useful in functions with multiple parameters, enhancing readability and reducing the amount of code needed for default value assignments.
Example: Multiple Default Parameters
Here, we define a function that requires multiple parameters, each with its own default value.
orderPizzahas two parameters, each with a default value:typedefaults to'Cheese', andsizedefaults to'Medium'.- Calling
orderPizza()without arguments uses both default values. - Calling
orderPizza('Pepperoni')uses the default value forsizebut overrides the default value fortype.
Passing Expressions as Default Parameters
Default parameters in JavaScript functions are not limited to static values; they can also be expressions. When an expression is used as a default parameter, it's evaluated at function call time if the parameter is omitted or undefined. This feature allows for dynamic initialization of function parameters based on runtime conditions or other function calls.
Example: Dynamic Default Parameter with Expression
In this example, we'll create a function that logs a message with a timestamp, using an expression as the default parameter to generate the timestamp dynamically.
function logMessage(message, timestamp = new Date().toLocaleTimeString()) { ... }defines a functionlogMessagewith two parameters:messageandtimestamp.messageis a required parameter for the message to log.timestamphas a default value that is an expression. Iftimestampis not provided when the function is called, JavaScript will executenew Date().toLocaleTimeString()to generate a default timestamp.
new Date().toLocaleTimeString()generates a string representing the current time according to the locale of the environment running the script. This expression is evaluated each time the function is called without atimestampargument, ensuring the timestamp reflects the call time.- When
logMessage("Hello, world!");is called without atimestamp, the function uses the current time as the default value fortimestampand logs the message with this dynamically generated timestamp.
On this page
Why We Need Default Parameters?
Example: Function Without Default Parameters
Example: Function With Default Parameters
Default Parameters with Multiple Arguments
Example: Multiple Default Parameters
Passing Expressions as Default Parameters
Example: Dynamic Default Parameter with Expression